package com.chen.palmar.common.widget.dialog;

import android.graphics.Color;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bruce.pickerview.LoopView;
import com.chen.palmar.R;

import java.util.ArrayList;
import java.util.List;

import me.shaohui.bottomdialog.BaseBottomDialog;


/**
 * Created by Chen on 2017/6/9.
 * 功能描述：
 */

public class SelectPickerDialog<T> extends BaseBottomDialog {
    public TextView cancelBtn;
    public TextView confirmBtn;
    public LoopView selectLoopView;
    public List<String> dataList = new ArrayList();

    private String textCancel = "取消";
    private String textConfirm = "确认";
    private int colorCancel = Color.parseColor("#999999");
    private int colorConfirm = Color.parseColor("#ff5900");
    ;
    private OnPickedListener mListener;
    private int selectPosition = 0;
    private int viewTextSize = 16;
    private int btnTextSize = 14;
    private List<T> objectList;
    private ProgressBar progressBar;

    public SelectPickerDialog() {
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_condition;
    }

    @Override
    public void bindView(View v) {
        cancelBtn = (TextView) v.findViewById(R.id.tv_dialog_cancel);
        confirmBtn = (TextView) v.findViewById(R.id.tv_dialog_confirm);
        selectLoopView = (LoopView) v.findViewById(R.id.loop_dialog_select);
        progressBar = (ProgressBar) v.findViewById(R.id.loading_progress);
        progressBar.setVisibility(View.VISIBLE);
        selectLoopView.setVisibility(View.GONE);
        cancelBtn.setOnClickListener(view -> this.dismiss());
        confirmBtn.setOnClickListener(view -> {
            this.dismiss();
            mListener.onPickCompleted(objectList.get(selectLoopView.getSelectedItem()));
        });
        selectLoopView.setInitPosition(selectPosition);
        selectLoopView.setCanLoop(false);
        selectLoopView.setDataList((ArrayList) dataList);
        cancelBtn.setText(textCancel);
        confirmBtn.setText(textConfirm);
        cancelBtn.setTextColor(colorCancel);
        confirmBtn.setTextColor(colorConfirm);
        selectLoopView.setTextSize(viewTextSize);
        confirmBtn.setTextSize(btnTextSize);
        cancelBtn.setTextSize(btnTextSize);
    }

    @Override
    public float getDimAmount() {
        return 0.5f;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public SelectPickerDialog setDateList(List<T> dataList) {
        this.dataList.clear();
        for (int i = 0; i < dataList.size(); i++) {
            this.dataList.add(dataList.get(i).toString());
        }
        selectLoopView.setDataList(this.dataList);
        selectLoopView.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
        objectList = dataList;
        return this;
    }

    public SelectPickerDialog setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        return this;
    }

    public SelectPickerDialog textCancel(String textCancel) {
        this.textCancel = textCancel;
        return this;
    }

    public SelectPickerDialog textConfirm(String textConfirm) {
        this.textConfirm = textConfirm;
        return this;
    }

    public SelectPickerDialog colorCancel(int colorCancel) {
        this.colorCancel = colorCancel;
        return this;
    }

    public SelectPickerDialog colorConfirm(int colorConfirm) {
        this.colorConfirm = colorConfirm;
        return this;
    }

    /**
     * set btn text btnTextSize
     *
     * @param textSize dp
     */
    public SelectPickerDialog btnTextSize(int textSize) {
        this.btnTextSize = textSize;
        return this;
    }

    public SelectPickerDialog viewTextSize(int textSize) {
        this.viewTextSize = textSize;
        return this;
    }

    public SelectPickerDialog setListener(OnPickedListener listener) {
        this.mListener = listener;
        return this;
    }

    public interface OnPickedListener<T> {

        /**
         * Listener when date has been checked
         *
         * @param dataDesc 被选中的条目
         */
        void onPickCompleted(T dataDesc);
    }
}
